package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("link")
public class LinkController {
    @Autowired
    private ILinkService service;

    @RequestMapping("findAll")
    public String findAll(@RequestParam(value = "pageNO",defaultValue = "1") int pageNO,
                          HttpServletRequest request){
        PageInfo<LinkBean> pageInfo = service.findAll(pageNO);
        request.setAttribute("pageObj",pageInfo);
        return "/html/link/index.html";
    }

    @RequestMapping("add")
    public String add(LinkBean link){
        service.add(link);
        return "redirect:/link/findAll";
    }

    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/link/findAll";
    }

    @RequestMapping("findById")
    public String findById(int id, Model model){
        LinkBean linkObj = service.findById(id);
        model.addAttribute("linkObj",linkObj);
        return "/html/link/update.html";
    }

    @RequestMapping("update")
    public String update(int id,String phone){
        service.update(id,phone);
        return "redirect:/link/findAll";
    }
}
